// pages/index/choose/index.js
const app = getApp()
const QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
let qqmapsdk;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        citys: [],
        cityname: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(wx.getStorageSync('city'))
        const that = this
        qqmapsdk = new QQMapWX({
            key: 'YN6BZ-BITW3-QGM3R-3DECZ-MAUZH-DBFDA'
        });
        wx.getLocation({
            success: function (res) {
                wx.setStorageSync("openSet", false);
                that.getLocal(res.latitude, res.longitude);
            },
            fail: function (fail) {},
        });
        app.http.post(
            app.api.city, {}, (res) => {
                that.setData({
                    citys: res.data
                })
            }
        )


    },
    // 打开设置
    openSet() {
        var that = this;
        if (wx.getStorageSync("openSet")) {
            wx.getSetting({
                success: (res) => {
                    var statu = res.authSetting;
                    if (!statu["scope.userLocation"]) {
                        //没授权
                        wx.showModal({
                            title: '授权',
                            content: '如果需要使用定位功能，请点击确定并在授权管理中打开“设置”，打开定位功能。',
                            complete: (res) => {
                                if (res.confirm) {
                                    wx.openSetting({
                                        success: function (res) {
                                            if (res.authSetting["scope.userLocation"]) {
                                                wx.setStorageSync("openSet", false);
                                                that.onLoad();
                                                that.init();
                                            } // 如果成功打开授权
                                            else {
                                                wx.setStorageSync("openSet", true);
                                                that.setData({
                                                    latitude: 0,
                                                    longitude: 0,
                                                });
                                                that.init();
                                            } // 如果用户依然拒绝授权
                                        },
                                        fail: function () {
                                            //调用失败，授权登录不成功
                                            return;
                                        },
                                    });
                                }

                                if (res.cancel) {
                                    that.setData({
                                        latitude: 0,
                                        longitude: 0,
                                    });
                                    that.init();
                                }
                            }
                        })

                    }
                },
            });
        }
    },
    choose() {
        wx.setStorageSync('city', this.data.cityname)
        wx.reLaunch({
            url: '/pages/index/index',
        })
    },
    getLocal(latitude, longitude) {
        console.log(latitude, longitude)
        let that = this;
        qqmapsdk.reverseGeocoder({
            location: {
                latitude: Number(latitude),
                longitude: Number(longitude)
            },
            success: function (res) {
                console.log(11, res);
                let city = res.result.ad_info.city
                that.setData({
                    cityname: city
                })
            },

            fail: function (res) {
                console.log(res);
            },
            complete: function (res) {
                // console.log(res);
            }
        });
    },
    chooseCity(e) {
        console.log(22, e)
        wx.setStorageSync('city', e.currentTarget.dataset.data)
        wx.setStorageSync('chooseCity', true)
        wx.reLaunch({
            url: '/pages/index/index',
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})